﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using com.kmer.Helper;
using com.kmer.interfaces;
using kmer.Web;

namespace com.kmer.library.Helper {
	/// <summary>
	/// 字符串资源管理
	/// </summary>
	public static class StringTableHelper {
		private static IDictionary<string, string> StringTable;

		private static void InitStringTable() {
			if (StringTable == null) {
				var str = myCache.GetFile("StringTable", "~/App_Data/StringTable.json");
				StringTable = str.FromJson<IDictionary<string, string>>();
			}
		}
		private static string Get(string pKey) {
			InitStringTable();
			if (!StringTable.ContainsKey(pKey))
				return string.Empty;
			return StringTable[pKey];
		}
		/// <summary>
		/// 判断是否存在
		/// </summary>
		/// <param name="pKey"></param>
		/// <returns></returns>
		public static bool Exist(string pKey) {
			InitStringTable();
			return StringTable.ContainsKey(pKey);
		}
		/// <summary>
		/// 根据字符串关键字读取字符串
		/// </summary>
		/// <param name="context"></param>
		/// <param name="pKey"></param>
		/// <param name="dv"></param>
		/// <returns></returns>
		public static string GetString(this ISessionContext context, string pKey, string dv) {
			if (!Exist(pKey))
				return dv;
			return Get(pKey);
		}
		/// <summary>
		/// 根据字符串关键字读取字符串
		/// </summary>
		/// <param name="pKey"></param>
		/// <param name="dv"></param>
		/// <returns></returns>
		public static string GetString(string pKey, string dv) {
			if (!Exist(pKey))
				return dv;
			return Get(pKey);
		}
	}
}
